package com.shiguo365.market.display.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shiguo365.market.R;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.util.ToastUtils;
import com.shiguo365.market.presenter.contract.ICartPresenter;
import com.shiguo365.market.presenter.implement.CartPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by caomenglong on 16/7/7.
 */
public class UserCartAdapter extends RecyclerView.Adapter<UserCartAdapter.ViewHolder> {

    private final static String TAG = "CartAdapter";
    private final Activity mActivity;
    private final LayoutInflater mInflater;
    private final List<JSONObject> objects;
    private ICartPresenter cartPresenter;

    public UserCartAdapter(@NonNull Activity activity, @NonNull ICartPresenter cartPresenter, @NonNull List<JSONObject> objects) {
        this.mActivity = activity;
        mInflater = LayoutInflater.from(activity);
        this.objects = objects;
        this.cartPresenter = cartPresenter;
    }

    @Override
    public int getItemCount() {
        return objects.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_cart, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.update(position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private JSONObject object;

        protected ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @BindView(R.id.cart_item_tv_title)
        protected TextView mTvTitle;

        @BindView(R.id.cart_item_tv_spec)
        protected TextView mTvSpec;

        @BindView(R.id.cart_item_edt_count)
        protected EditText mEdtCount;

        @BindView(R.id.imageViewCartImage)
        protected ImageView mImageView;

        @BindView(R.id.icart_item_tv_inventory)
        protected TextView mTvInventory;

        @BindView(R.id.icart_item_tv_price1)
        protected TextView mTvPrice1;


        @BindView(R.id.pircediv2)
        protected LinearLayout pricelinearLayout2;

        @BindView(R.id.icart_item_tv_title2)
        protected TextView mTvPriceTitle2;

        @BindView(R.id.icart_item_tv_price2)
        protected TextView mTvPrice2;

        @BindView(R.id.pircediv3)
        protected LinearLayout pricelinearLayout3;

        @BindView(R.id.icart_item_tv_title3)
        protected TextView mTvPriceTitle3;

        @BindView(R.id.icart_item_tv_price3)
        protected TextView mTvPrice3;


        //删除商品
        @OnClick(R.id.imageButtonCartTrash)
        protected void CartTrash() {
            cartPresenter.deleteCart(MyApplication.getUID(), object.getInteger("id"));
        }

        //添加商品数量
        @OnClick(R.id.imageButtonCartMinus)
        protected void CartMinus() {

            Integer number=Integer.valueOf(mEdtCount.getText().toString());
            if(number-1>0) {
                cartPresenter.updateCartNumber(MyApplication.getUID(), object.getInteger("id"), -1,null);
            }

        }

        //减少商品数量
        @OnClick(R.id.imageButtonCartPlus)
        protected void CartPlus() {
            Integer number=Integer.valueOf(mEdtCount.getText().toString());
            if(number<object.getInteger("inventory")) {
                cartPresenter.updateCartNumber(MyApplication.getUID(), object.getInteger("id"), 1, null);
            }
        }


        View.OnKeyListener onKeyListener =new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                Integer number=Integer.valueOf(mEdtCount.getText().toString());
                ToastUtils.with(MyApplication.getAppContext()).show(number.toString());
                if (keyCode == EditorInfo.IME_ACTION_DONE) {

                    if(number>0){
                        cartPresenter.updateCartNumber(MyApplication.getUID(), object.getInteger("id"), null, number);
                    }
                }
                return false;

            }
        };

        TextView.OnEditorActionListener onEditorActionListener = new  TextView.OnEditorActionListener(){

            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if(actionId==EditorInfo.IME_ACTION_DONE){
                    try {
                        Integer number= Integer.valueOf(mEdtCount.getText().toString());
                        if(number>0 ){
                            cartPresenter.updateCartNumber(MyApplication.getUID(), object.getInteger("id"), null, number);
                        }else {
                            mEdtCount.setText("1");
                            number=1;
                            cartPresenter.updateCartNumber(MyApplication.getUID(), object.getInteger("id"), null, number);
                        }
                    }catch (Exception e){
                        cartPresenter.updateCartNumber(MyApplication.getUID(), object.getInteger("id"), null, 1);
                    }

                }
                return false;
            }
        };


        protected void update(int position) {
            object = objects.get(position);

            mTvTitle.setText(object.getString("name"));
            mTvSpec.setText(object.getString("quantity"));
            mEdtCount.setText(object.getInteger("pnum").toString());
            mEdtCount.setOnEditorActionListener(onEditorActionListener);
            mTvInventory.setText(object.getInteger("inventory").toString() + "份");
            mTvPrice1.setText("￥" + object.getString("price"));

            JSONObject jsonObject = object.getJSONObject("more_buy");
            Log.d(TAG, object.toJSONString());
            Log.d(TAG, jsonObject.toJSONString());

            if (jsonObject.size() > 0) {
                if (jsonObject.getInteger("num1") > 0) {
                    mTvPriceTitle2.setText("≥" + jsonObject.getInteger("num1").toString() + "件");
                    mTvPrice2.setText("￥" + jsonObject.getInteger("price1").toString());
                } else {
                    pricelinearLayout2.setVisibility(View.GONE);
                }
                if (jsonObject.getInteger("num2") > 0) {
                    mTvPriceTitle3.setText("≥" + jsonObject.getInteger("num2").toString() + "件");
                    mTvPrice3.setText("￥" + jsonObject.getInteger("price2").toString());
                } else {
                    pricelinearLayout3.setVisibility(View.GONE);
                }
            } else {
                pricelinearLayout2.setVisibility(View.GONE);
                pricelinearLayout3.setVisibility(View.GONE);
            }


            ImageLoader imageLoader = ImageLoader.getInstance();
            imageLoader.displayImage(object.getString("main_pic"), mImageView);

            Log.d(TAG,"NA?");


        }
    }
}
